package com.inxedu.os.edu.service.systemMsg.systemMsgImpl;

import com.inxedu.os.edu.dao.EduMsgReceiveMapper;
import com.inxedu.os.edu.dao.EduUserMapper;
import com.inxedu.os.edu.pojo.EduMsgReceive;
import com.inxedu.os.edu.pojo.EduUser;
import com.inxedu.os.edu.pojo.LetterConstans;
import com.inxedu.os.edu.service.systemMsg.MsgReceiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MsgReceiveServiceImpl implements MsgReceiveService {
    @Autowired
    private EduMsgReceiveMapper eduMsgReceiveMapper;
    @Autowired
    private EduUserMapper eduUserMapper;

    /**
     * 发送系统消息
     *
     * @param content 要发送的内容
     * @param cusId   用户id
     * @throws Exception
     */
    @Override
    public String addSystemMessageByCusId(String content, Long cusId) {
        EduUser user = eduUserMapper.selectByPrimaryKey(cusId);
        EduMsgReceive eduMsgReceive=new EduMsgReceive();
        eduMsgReceive.setContent(content);
        eduMsgReceive.setCusId(Long.valueOf(0));
        eduMsgReceive.setReceivingCusid(cusId);
        eduMsgReceive.setStatus(LetterConstans.LETTER_STATUS_UNREAD);// 消息未读状态
        eduMsgReceive.setType(LetterConstans.LETTER_TYPE_SYSTEMINFORM);// 系统消息
        eduMsgReceive.setAddTime(new Date());
        eduMsgReceive.setUpdateTime(new Date());
        if(user!=null&&user.getShowName()!=null){// 如果不为空则set showname
            eduMsgReceive.setShowname(user.getShowName());//会员名
        }else {// 如果为空则set 空字符串
            eduMsgReceive.setShowname("");//会员名
        }
        eduMsgReceiveMapper.insertSelective(eduMsgReceive);
        eduUserMapper.updateUnReadMsgNumAddOne("sysMsgNum",cusId);
        return "success";
    }
}
